﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Analyses;
using PH.Model.Analyses.Schools;
using PH.Logic.Analyses;
namespace PH.Web.Management.Areas.Services.Controllers
{
    [RouteArea("Services")]
    [RoutePrefix("BySchool")]
    public class BySchoolApiController : ServiceBase
    {
        [Route("GetS4Class")]
        [HttpPost]
        public JsonEntitySet<S4ClassModel> GetS4Class(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    AnalysisQuery sea = new AnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    var ret = SchoolLogic.GetS4Class(sea);
                    return new JsonEntitySet<S4ClassModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4ClassModel>(null, errinfo, false);
        }

        [Route("GetS4Sex")]
        [HttpPost]
        public JsonEntitySet<S4SexModel> GetS4Sex(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    AnalysisQuery sea = new AnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    var ret = SchoolLogic.GetS4Sex(sea);
                    return new JsonEntitySet<S4SexModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4SexModel>(null, errinfo, false);
        }

        [Route("GetS4Grade")]
        [HttpPost]
        public JsonEntitySet<S4GradeModel> GetS4Grade(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    AnalysisQuery sea = new AnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    var ret = SchoolLogic.GetS4Grade(sea);
                    return new JsonEntitySet<S4GradeModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4GradeModel>(null, errinfo, false);
        }

        [Route("GetS4Teacher")]
        [HttpPost]
        public JsonEntitySet<S4TeacherModel> GetS4Teacher(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    AnalysisQuery sea = new AnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    var ret = SchoolLogic.GetS4Teacher(sea);
                    return new JsonEntitySet<S4TeacherModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4TeacherModel>(null, errinfo, false);
        }

        [Route("GetS4SampleGroup")]
        [HttpPost]
        public JsonEntitySet<S4SampleGroupModel> GetS4SampleGroup(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    S4SampleQuery sea = new S4SampleQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    sea.Finish = request.Value<bool?>("finished");
                    var ret = SchoolLogic.GetS4SampleGroup(sea);
                    return new JsonEntitySet<S4SampleGroupModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4SampleGroupModel>(null, errinfo, false);
        }

        [Route("GetS4Sample")]
        [HttpPost]
        public JsonEntitySet<S4SampleModel> GetS4Sample(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    S4SampleQuery sea = new S4SampleQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    sea.ClassID = request.Value<int>("classID");
                    var ret = SchoolLogic.GetS4Sample(sea);
                    return new JsonEntitySet<S4SampleModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4SampleModel>(null, errinfo, false);
        }

        [Route("GetS4Student")]
        [HttpPost]
        public JsonEntitySet<S4StudentModel> GetS4Student(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.School != null)
                {
                    S4StudentQuery sea = new S4StudentQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.ClassID = request.Value<int>("classID");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.SchoolGuid = user.School.Guid;
                    sea.ClassID = request.Value<int>("classID");
                    var ret = SchoolLogic.GetS4Student(sea);
                    return new JsonEntitySet<S4StudentModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<S4StudentModel>(null, errinfo, false);
        }
    }
}
